package com.myx.servlet.admin;

import com.myx.pojo.User;
import com.myx.service.user.UserService;
import com.myx.service.user.UserServiceImpl;
import com.myx.tool.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminLoginServlet",value = "/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String username= request.getParameter("uname");
        String password=request.getParameter("upassword");
        User user = userService.getAdminUserByUsernameAndPassword(username,password);
        if (user != null) {
            request.getSession().setAttribute(Constant.LOGIN_SESSION,user);
            response.sendRedirect(request.getContextPath()+"/jsp/admin/news/index.jsp");
        }else {
            request.setAttribute("admin_login_error","错误，重新输入");
            request.getRequestDispatcher("/jsp/admin/login.jsp").forward(request,response);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
